
integer function gcd(x,y)
    implicit none
    integer x,y,c,rest

    if (x .EQ. y) then
        gcd=x
    elseif (y .LT. x) then
        c=x
        x=y
        y=c
        !exchange x and y st. y is greater
        10 rest=mod(y,x)
            y=x
            x=rest
            if (mod(y,x) .EQ. 0) then
                gcd=x
            else
                goto 10
            end if
    else
        20 rest=mod(y,x)
            y=x
            x=rest
            if (mod(y,x) .EQ. 0) then
                gcd=x
            else
                goto 20
            end if

    end if

end

program main

    !GCD compose greatest common divisor
    !using a function
    implicit none
    integer a,b
    integer gcd
    print *, 'Calculate greatest common divisor of two integers'
    print *, 'please input two integers'
    read(*,*) a,b
    print *, 'GCD is', gcd(a,b)

end program


